package com.wanwu.channel.remote.allinpay.TLHttpUtils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignMacUtil {

    private static final char[] DIGITS_LOWER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    public final static String SIGNALG = "GCP1-HMAC-SHA256";
    public final static String HMACALG = "HmacSHA256";
    public final static String DGSTALG = "SHA-256";
    public final static String HMCHST = "UTF-8";

    public static String strSign(String date, String uri, String queryStr, byte[] digest) {
        StringBuilder sb = new StringBuilder();
        sb.append(SIGNALG).append("\n").append(date).append("\n").append(uri).append("\n")
                .append(queryStr).append("\n").append(encodeHex(digest));
        return sb.toString();
    }


    public static byte[] digest(String body) {
        try {
            return digest(body.getBytes(HMCHST));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] digest(byte[] body) {
        try {
            String alg = DGSTALG;
            MessageDigest messageDigest;
            messageDigest = MessageDigest.getInstance(alg);
            messageDigest.update(body);
            return messageDigest.digest();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    public static char[] encodeHex(final byte[] data) {
        final char[] toDigits = DIGITS_LOWER;
        final int l = data.length;
        final char[] out = new char[l << 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; i < l; i++) {
            out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
            out[j++] = toDigits[0x0F & data[i]];
        }
        return out;
    }

    public static byte[] decodeHex(final char[] data) {
        final int len = data.length;
        if ((len & 0x01) != 0) {
            throw new RuntimeException("Odd number of characters.");
        }
        final byte[] out = new byte[len >> 1];
        // two characters form the hex value.
        for (int i = 0, j = 0; j < len; i++) {
            int f = Character.digit(data[j], 16) << 4;
            j++;
            f = f | Character.digit(data[j], 16);
            j++;
            out[i] = (byte) (f & 0xFF);
        }
        return out;
    }


    public static String hmac(String kSecret, String day, String scop, String data) {
        if (day.length() > 8) day = day.substring(0, 8);
        byte[] hmac = hmacb(kSecret, day, scop, data);
        return new String(encodeHex(hmac));
    }

    public static byte[] hmacb(String kSecret, String day, String scop, String data) {
        try {
            String c = HMCHST;
            return hmac(kSecret.getBytes(c), day.getBytes(c), scop.getBytes(c), data.getBytes(c));
        } catch (Exception e) {
            if (e instanceof RuntimeException) throw (RuntimeException) e;
            else throw new RuntimeException(e);
        }
    }

    public static byte[] hmac(byte[] kSecret, byte[] day, byte[] scop, byte[] data) {
        return hmac(hmac(hmac(kSecret, day), scop), data);
    }

    public static byte[] hmac(byte[] key, byte[] data) {
        try {
            String alg = HMACALG;
            Mac mac = Mac.getInstance(alg);
            mac.init(new SecretKeySpec(key, alg));
            return mac.doFinal(data);
        } catch (Exception e) {
            if (e instanceof RuntimeException) throw (RuntimeException) e;
            else throw new RuntimeException(e);
        }
    }

}
